package com.jewelry.crm.model.dao;


import com.jewelry.crm.common.ModelValidationException;
import com.jewelry.crm.model.domain.Supplier;
import org.hibernate.Session;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.jewelry.crm.common.Common.logger;
import static com.jewelry.crm.model.dao.HibernateUtil.getSessionFactory;
import static com.jewelry.crm.model.dao.HibernateUtil.getValidator;
import static com.jewelry.crm.model.dao.HibernateUtil.validate;

public class SupplierDaoImpl implements SupplierDao{

    public void addSupplier(Supplier supplier) throws SQLException {
        try {
            if (validate(supplier, getValidator())) {
                Session session = getSessionFactory().openSession();
                session.beginTransaction();
                session.save(supplier);
                session.getTransaction().commit();
                session.close();
            } else throw new ModelValidationException("Supplier wasn't added due to validation errors");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    public void updateSupplier(Supplier supplier) throws SQLException {
        try {
            if (validate(supplier, getValidator())) {
                Session session = getSessionFactory().openSession();
                session.beginTransaction();
                session.update(supplier);
                session.getTransaction().commit();
                session.close();
            } else throw new ModelValidationException("Supplier wasn't updated due to validation errors");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    public Supplier getSupplierById(Long supplier_id) throws SQLException {
        try {
            Session session = getSessionFactory().openSession();
            Supplier supplier = (Supplier) session.get(Supplier.class, supplier_id);
            session.close();
            return supplier;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new Supplier();
        }
    }

    public Collection getAllSuppliers() throws SQLException {
        try {
            Session session = getSessionFactory().openSession();
            List suppliers = session.createCriteria(Supplier.class).list();
            session.close();
            return suppliers;
        }
        catch (Exception e) {
            logger.error(e.getMessage());
            return new ArrayList();
        }
    }

    public void deleteSupplier(Supplier supplier) throws SQLException {
        try {
            Session session = getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(supplier);
            session.getTransaction().commit();
            session.close();
        }
        catch (Exception e) {
            logger.error(e.getMessage());
        }
    }


}
